local kl__sheshu = fk.CreateSkill {
  name = "kl__sheshu",
  max_branches_use_time = {
    ["draw"] = {
      [Player.HistoryPhase] = 1,
    },
    ["loseHp"] = {
      [Player.HistoryPhase] = 1,
    }
  },
}

Fk:loadTranslationTable{
  ["kl__sheshu"] = "射书",
  [":kl__sheshu"] = "出牌阶段每项各限一次，你可以将任意张手牌交给一名其他角色并选择一项："..
  "<br>1.其复原武将牌，你摸X+1张牌（X为其手牌中未拥有的花色数）；"..
  "<br>2.其失去一点体力获得一点护甲，若你交给其的牌的最大点数不小于其原手牌的最大点数，"..
  "则该角色的下个回合内手牌上限-1且摸牌阶段结束时须弃Y+1张牌（Y为你交给其的牌数）。",

  ["#kl__sheshu"] = "射书：出牌阶段每项各限一次，你可以将任意张手牌交给一名其他角色，且：",
  ["#kl__sheshu-1"] = "其复原武将牌，你摸牌",
  ["#kl__sheshu-2"] = "其失去1体力获得1护甲并获得减益",
  ["@kl__sheshu"] = "射书",

  ["$kl__sheshu1"] = "严规重纪，此吾治军之道。",
}

kl__sheshu:addEffect("active", {
  anim_type = "control",
  min_card_num = 1,
  target_num = 1,
  prompt = "#kl__sheshu",
  interaction = function(self, player)
    local all__choices = {"#kl__sheshu-1", "#kl__sheshu-2"}
    local choice = {}
    if kl__sheshu:withinBranchTimesLimit(player, "draw", Player.HistoryPhase) then
      table.insert(choice, "#kl__sheshu-1")
    end
    if kl__sheshu:withinBranchTimesLimit(player, "loseHp", Player.HistoryPhase) then
      table.insert(choice, "#kl__sheshu-2")
    end
    return UI.ComboBox { choices = choice, all__choices = all__choices }
  end,
  card_filter = function(self, player, to_select, selected)
    return table.contains(player:getHandlyIds(), to_select)
  end,
  can_use = function (self, player)
    return kl__sheshu:withinBranchTimesLimit(player, "draw", Player.HistoryPhase) or kl__sheshu:withinBranchTimesLimit(player, "loseHp", Player.HistoryPhase)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = effect.cards
    local target_cards = target:getCardIds("h")
    room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. "kl__jimu", player:getSwitchSkillState("kl__jimu", true))
    room:notifySkillInvoked(player, "kl__jimu", "switch")
    player:clearSkillHistory("kl__jimu")
    room:obtainCard(target, cards, false, fk.ReasonGive, player, kl__sheshu.name)
    if self.interaction.data == "#kl__sheshu-1" then
      player:addSkillBranchUseHistory(kl__sheshu.name, "draw", 1)
      target:reset()
      local suits = {}
      for _, id in ipairs(target_cards) do
        table.insertIfNeed(suits, Fk:getCardById(id).suit)
      end
      room:drawCards(player, 5 - #suits, kl__sheshu.name)
    elseif self.interaction.data == "#kl__sheshu-2" then
      player:addSkillBranchUseHistory(kl__sheshu.name, "loseHp", 1)
      if target.dead then return end
      room:loseHp(target, 1, kl__sheshu.name)
      if target.dead then return end
      room:changeShield(target, 1)
      local self_max = 0
      local to_max = 0
      for _, id in ipairs(target_cards) do
        local card = Fk:getCardById(id)
        if card.number > to_max then
          to_max = card.number
        end
      end
      for _, id in ipairs(cards) do
        local card = Fk:getCardById(id)
        if card.number > self_max then
          self_max = card.number
        end
      end
      if not target.dead and self_max >= to_max then
        local mark = target:getTableMark("@kl__sheshu")
        if next(mark) == nil then
          mark = {0, 0}
        end
        mark[1] = mark[1] - 1
        mark[2] = mark[2] + #cards + 1
        room:setPlayerMark(target, "@kl__sheshu", mark)
      end
    end
  end,
})

kl__sheshu:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and #player:getTableMark("@kl__sheshu") > 0
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "@kl__sheshu", 0)
  end,
})

kl__sheshu:addEffect("maxcards", {
  correct_func = function(self, player)
    if #player:getTableMark("@kl__sheshu") > 0 and Fk:currentRoom():getCurrent() == player then
      return player:getTableMark("@kl__sheshu")[1]
    end
  end,
})

kl__sheshu:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(kl__sheshu.name) and #target:getTableMark("@kl__sheshu") > 0 and target.phase == Player.Draw and not target:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = target:getTableMark("@kl__sheshu")[2]
    room:askToDiscard(target, {
      min_num = num,
      max_num = num,
      skill_name = kl__sheshu.name,
      cancelable = false,
      include_equip = true,
    })
  end,
})


return kl__sheshu
